<template>
  <div class='container'>
    <el-dialog
      :title="title"
      :visible="dialogVisible"
      width="20%"
      @close="handleClose"
      @open="open"
    >
      <el-form
        ref="formRef"
        :model="ruleForm"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item
          v-if="!this.subjectID"
          label="所属学科"
          prop="subjectID"
        >
          <el-select
            v-model="ruleForm.subjectID"
            style="width: 100%;"
          >
            <el-option
              v-for="item in subjectList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="目录名称"
          prop="directoryName"
        >
          <el-input
            v-model.trim="ruleForm.directoryName"
            placeholder="请输入目录名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleClose">取 消</el-button>
          <el-button
            type="primary"
            @click="handleClose(1)"
          >确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects'
import { add, update } from '@/api/hmmm/directorys'
export default {
  name: 'tags-add',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      subjectList: [], // 学科列表
      ruleForm: {
        id: null,
        subjectID: '',
        directoryName: ''
      },
      rules: {
        subjectID: [
          { required: true, message: '请选择学科名称', trigger: ['change', 'blur'] }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  computed: {
    title () {
      return this.ruleForm.id ? '修改目录' : '新增目录'
    },
    subjectID () {
      return this.$route.query.id
    }
    // subjectName () {
    //   return this.$route.query.name
    // }
  },
  methods: {
    async open () {
      if (this.subjectID) return
      // console.log('打开弹窗执行')
      const res = await simple()
      // console.log(res)
      this.subjectList = res.data
    },
    handleClose (val) {
      if (val === 1) {
        // 校验
        // 如果有关键字 subjectID 则重新赋值
        if (this.subjectID) {
          this.ruleForm.subjectID = +this.subjectID
        }

        this.$refs.formRef.validate(async valid => {
          if (!valid) return this.$message.error('请填写完整信息')
          // console.log('确定')

          if (this.ruleForm.id) {
            console.log('修改')
            const res = await update(this.ruleForm)
            // console.log(res)
            if (res.status !== 200) return this.$message.error('修改失败')
            this.$message.success('修改成功')
          } else {
            console.log('新增')
            const res = await add(this.ruleForm)
            //   console.log(res)
            if (res.status !== 200) return this.$message.error('新增失败')
            this.$message.success('新增成功')
          }
          this.$emit('update:dialogVisible', false)
          this.$emit('update')
        })
      } else {
        this.$refs.formRef.resetFields()
        this.ruleForm = { id: null, subjectID: '', directoryName: '' }
        this.$emit('update:dialogVisible', false)
      }
    }
  }
}
</script>

<style scoped lang='scss'></style>
